package com.cxm.controller;

import com.cxm.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.io.Serializable;

@Controller
public class HtmlController {

    @GetMapping("/hudong/userchart")
    public String redirctor(){
        return "userchart";
    }
    @GetMapping("/hudong/flowchart")
    public String redirctor44(){
        return "flowchart";
    }
    @GetMapping("/hudong/index")
    public String redirctor2(){
        return "index";
    }
    @GetMapping("/hudong/lotterychart")
    public String redirctor3(){
        return "lotterychart";
    }

    @GetMapping("/text")
    public String redirctor5(){
        return "text";
    }

    @GetMapping("/hudong/welcome")
    public ModelAndView redirect432() {
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.setViewName("welcome");
//        modelAndView.addObject("videokindlist",videokindService.list());
        return modelAndView;
    }

    @GetMapping("/denglu")
    public String denglu() {

        return "denglu";
    }
    @GetMapping("/")
    public String denglu2() {

        return "denglu";
    }

    //登录模块

    @PostMapping("/login")
    @ResponseBody
    public Serializable login(String username, String password){

        Subject subject= SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
        try {
//验证密码是否正确
            subject.login(token);
            User user= (User) subject.getPrincipal();
            subject.getSession().setAttribute("user",user);
//            session.setAttribute("userinfo",userinfo);

            return 0;
        } catch (UnknownAccountException e) {
            e.printStackTrace();

            return "没有此用户";
        } catch(IncorrectCredentialsException e){
            e.printStackTrace();

            return  "密码错误";
        }



    }











































}
